package com.hewep.view;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

import javafx.embed.swing.SwingNode;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import com.hewep.support.print.Printer;

public class PrintPreviewPane extends AnchorPane{
	private PreviewCanvas canvas;
	
	public PrintPreviewPane(Printer pt, String str) {
		// TODO Auto-generated constructor stub
		canvas = new PreviewCanvas(pt, str);
		init();
	}
	
	private void init() {
		final SwingNode swingNode = new SwingNode();
        createAndSetSwingContent(swingNode);
        
        StackPane stack = new StackPane();
        stack.getChildren().add(swingNode);
		this.getChildren().add(stack);
		AnchorPane.setBottomAnchor(stack, 0.0);
		AnchorPane.setTopAnchor(stack, 0.0);
		AnchorPane.setLeftAnchor(stack, 0.0);
		AnchorPane.setRightAnchor(stack, 0.0);
		
		Stage stage = new Stage(StageStyle.UTILITY);
		stage.initModality(Modality.APPLICATION_MODAL);
		stage.setScene(new Scene(this, 600, 500));
		stage.show();

	}
	private void createAndSetSwingContent(final SwingNode swingNode) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                swingNode.setContent(canvas);
            }
        });
    }
	
	class PreviewCanvas extends JPanel {
		private String printStr;
		private int currentPage = 0;
		private Printer preview;

		public PreviewCanvas(Printer pt, String str) {
			printStr = str;
			preview = pt;
		}

		public void paintComponent(Graphics g) {
			super.paintComponent(g);
			Graphics2D g2 = (Graphics2D) g;
			PageFormat pf = PrinterJob.getPrinterJob().defaultPage();
			// 通过Paper设置页面的空白边距和可打印区域。必须与实际打印纸张大小相符。  
			Paper p = new Paper();
			p.setSize(preview.getWidth(), preview.getHeight());
			p.setImageableArea(preview.getOffsetX(), preview.getOffsetY(), preview.getWidth(), preview.getHeight()); // Print Area
			pf.setPaper(p);
			double xoff;
			double yoff;
			double scale;
			double px = pf.getWidth();
			double py = pf.getHeight();
			double sx = getWidth() - 1;
			double sy = getHeight() - 1;
			if (px / py < sx / sy) {
				scale = sy / py;
				xoff = 0.5 * (sx - scale * px);
				yoff = 0;
			} else {
				scale = sx / px;
				xoff = 0;
				yoff = 0.5 * (sy - scale * py);
			}
			g2.translate((float) xoff, (float) yoff);
			g2.scale((float) scale, (float) scale);
			Rectangle2D page = new Rectangle2D.Double(0, 0, px, py);
			g2.setPaint(Color.white);
			g2.fill(page);
			g2.setPaint(Color.black);
			g2.draw(page);

			try {
				preview.print(g2, pf, currentPage);
			} catch (PrinterException pe) {
				g2.draw(new Line2D.Double(0, 0, px, py));
				g2.draw(new Line2D.Double(0, px, 0, py));
			}
		}

	}
}
